package com.example.picvideodemo;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.picvideodemo.utils.StringUtils;

import java.util.ArrayList;

/**
 * date:2021/4/29
 * author:wsm(admin)
 * funcation:发布动态添加图片
 */
public class AddImageAdapter extends RecyclerView.Adapter<AddImageAdapter.ViewHolder> {

    private static final int TYPE_ADD = 1;
    private static final int TYPE_MEDIA = 2;

    private ArrayList<String> mMediaList = new ArrayList<>();
    private OnAddMediaListener mOnAddMediaListener;
    private Activity activity;
    private String filetype;

    public interface OnAddMediaListener {
        void onaddMedia();
    }

    public AddImageAdapter(OnAddMediaListener mOnAddMediaListener, Activity activity) {
        this.mOnAddMediaListener = mOnAddMediaListener;
        this.activity = activity;
    }

    public void setData(ArrayList<String> mediaList, String type) {
        mMediaList.clear();
        filetype = type;
        mMediaList.addAll(mediaList);
        notifyDataSetChanged();
    }

    public void clear() {
        mMediaList.clear();
        notifyDataSetChanged();
    }

    public ArrayList<String> getData() {
        return mMediaList;
    }

    public String gettype() {
        return filetype;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivPicture;
        LinearLayout llDelete;
        RelativeLayout rlItemImage;

        ViewHolder(View view) {
            super(view);
            ivPicture = (ImageView) view.findViewById(R.id.ivPinPicture);
            llDelete = (LinearLayout) view.findViewById(R.id.llPinDelete);
            rlItemImage = view.findViewById(R.id.rlPinItemImage);

        }
    }

    @Override
    public int getItemCount() {
        return mMediaList.size() == 0 ? 1 : mMediaList.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == mMediaList.size() || mMediaList.size() == 0) {
            return TYPE_ADD;
        } else {
            return TYPE_MEDIA;
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.moment_add_image, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) viewHolder.rlItemImage.getLayoutParams();
        int spex = StringUtils.dipToPx(activity, 70);
        int width = StringUtils.getScreenWidth(activity);
        layoutParams.width = (width - spex) / 3;
        layoutParams.height = (width - spex) / 3;
        viewHolder.rlItemImage.setLayoutParams(layoutParams);
        if (getItemViewType(position) == TYPE_ADD) {
            //发布图片最多选取9张
            if (filetype.equals("image")) {
                if (mMediaList.size() == 6) {
                    viewHolder.rlItemImage.setVisibility(View.GONE);
                } else {
                    viewHolder.rlItemImage.setVisibility(View.VISIBLE);
                    viewHolder.ivPicture.setImageResource(R.mipmap.icon_add_image);
                }
                viewHolder.ivPicture.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnAddMediaListener.onaddMedia();
                    }
                });
                viewHolder.llDelete.setVisibility(View.INVISIBLE);
            } else {
                //视频最多选取一个
                if (mMediaList.size() == 1) {
                    viewHolder.rlItemImage.setVisibility(View.GONE);
                } else {
                    viewHolder.rlItemImage.setVisibility(View.VISIBLE);
                    viewHolder.ivPicture.setImageResource(R.mipmap.icon_add_image);
                }
                viewHolder.ivPicture.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnAddMediaListener.onaddMedia();
                    }
                });

                viewHolder.llDelete.setVisibility(View.INVISIBLE);
            }

        } else {
            viewHolder.llDelete.setVisibility(View.VISIBLE);
            viewHolder.llDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int index = viewHolder.getAdapterPosition();
                    if (index != RecyclerView.NO_POSITION) {
                        mMediaList.remove(index);
//                        notifyItemRemoved(index);
//                        notifyItemRangeChanged(index, mMediaList.size());
                        notifyDataSetChanged();
                    }
                }
            });
            String path = mMediaList.get(position);
            Glide.with(viewHolder.itemView.getContext())
                    .load(path)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(viewHolder.ivPicture);
            //itemView 的点击事件

            if (mItemClickListener != null) {
                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int adapterPosition = viewHolder.getAdapterPosition();
                        mItemClickListener.onItemClick(filetype, adapterPosition);
                    }
                });
            }
        }
    }

    protected OnItemClickListener mItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(String type, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mItemClickListener = listener;
    }
}
